package ui.swing.table;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class Table_AbstractTbmImpl1 extends JFrame {

	private static final long serialVersionUID = -5637012473970333798L;

	public Table_AbstractTbmImpl1() {
		super("继承AbstractTableModel，改写getColumnClass()的TableModel");
		//setTitle("继承AbstractTableModel，改写getColumnClass()的TableModel");
		JTable table = new JTable(new MyTableModel());
		//table.setSize(new Dimension(200, 100));
		
		getContentPane().add(table.getTableHeader(), BorderLayout.NORTH);
		getContentPane().add(table, BorderLayout.CENTER);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(200, 50, 500, 500);
		//pack();
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new Table_AbstractTbmImpl1();
	}
	
	class MyTableModel extends AbstractTableModel {
		
		private static final long serialVersionUID = 9061362115301638066L;

		Object[][] p = {
				{"阿呆", new Integer(66), new Integer(32), new Integer(98), new Boolean(false), new Boolean(false)},
				{"阿瓜", new Integer(85), new Integer(69), new Integer(154), new Boolean(true),new Boolean(false)}
		};
		
		String[] n = {"姓名","语文","数学","总分","及格","作弊"};

		public int getColumnCount() {
			return n.length;
		}

		// getRowCount() is called frequently by JTable for display purposes
		// therefore, it should be designed with efficiency in mind.
		public int getRowCount() {
			return p.length;
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			return p[rowIndex][columnIndex];
		}

		// 重写这个方法定义每列的类型，用于分配默认的renderer和editor
		// used to assign a default renderer and editor for that column
		@Override
		public Class<?> getColumnClass(int columnIndex) {
			return getValueAt(0, columnIndex).getClass();
		}
		
		// 重写这个设置某单元格是否可以编辑，默认为全不可以(return false)
		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return super.isCellEditable(rowIndex, columnIndex);
		}
		
		// 重写这个说明如何设置单元格数据，实现时必须根据isCellEditable的情况
		// 默认实现为空
		@Override
		public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
			super.setValueAt(aValue, rowIndex, columnIndex);
		}
		
	}
}

